﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chat
{
    public abstract class Connection<InCmd,OutCmd> : InCmdOutCmd<InCmd,OutCmd>
        where InCmd : Command where OutCmd : Command
    {
        public Connection() : base()
        {
        }

        public abstract int ReceiveTimeout { get; set; }

        public abstract void Close();

        public abstract void SendCmd(
            OutCmd outCmd, CmdSerializer<InCmd,OutCmd> serializer);

        public abstract InCmd ReceiveCmd(
            CmdSerializer<InCmd,OutCmd> serializer);

        public ICmdReceiver<InCmd> GetCommandReceiver(
            CmdSerializer<InCmd, OutCmd> serializer)
        {
            return new CmdReceiver(this, serializer);
        }

        class CmdReceiver : ICmdReceiver<InCmd>
        {
            protected Connection<InCmd, OutCmd> Conn;
            protected CmdSerializer<InCmd, OutCmd> Ser;

            public CmdReceiver(
                Connection<InCmd, OutCmd> conn, CmdSerializer<InCmd, OutCmd> ser)
            {
                Conn = conn;
                Ser = ser;
            }
            #region ICmdReceiver<InCmd> Members

            public InCmd ReceiveCommand()
            {
                return Conn.ReceiveCmd(Ser);
            }

            #endregion
        }
    }
}
